<?php

function xmldb_block_guestbook_upgrade($oldversion = 0) {

    global $DB;
    $dbman = $DB->get_manager();
    
    $result = true;
    $actualversion = 2012101400;
    
    if ($oldversion < $actualversion) {

        // Define table block_guestbook to be created
        $table = new xmldb_table('block_guestbook');

        // Adding fields to table block_guestbook
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);

        // Adding keys to table block_guestbook
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

        // Conditionally launch create table for block_guestbook
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // guestbook savepoint reached
        upgrade_block_savepoint(true, $actualversion, 'guestbook');
        
        
        

        // Define table block_guestbook_messages to be created
        $table = new xmldb_table('block_guestbook_messages');

        // Adding fields to table block_guestbook_messages
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '24', null, null, null, null);
        $table->add_field('text', XMLDB_TYPE_CHAR, '1024', null, null, null, null);
        $table->add_field('timestamp', XMLDB_TYPE_INTEGER, '13', null, XMLDB_NOTNULL, null, null);

        // Adding keys to table block_guestbook_messages
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

        // Conditionally launch create table for block_guestbook_messages
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // guestbook savepoint reached
        upgrade_block_savepoint(true, $actualversion, 'guestbook');
        
        
        

        // Define table block_guestbook_comments to be created
        $table = new xmldb_table('block_guestbook_comments');

        // Adding fields to table block_guestbook_comments
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('msgid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '24', null, null, null, null);
        $table->add_field('text', XMLDB_TYPE_CHAR, '1024', null, null, null, null);
        $table->add_field('timestamp', XMLDB_TYPE_INTEGER, '13', null, XMLDB_NOTNULL, null, null);

        // Adding keys to table block_guestbook_comments
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('msgid', XMLDB_KEY_FOREIGN, array('msgid'), 'block_guestbook_messages', array('id'));

        // Conditionally launch create table for block_guestbook_comments
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // guestbook savepoint reached
        upgrade_block_savepoint(true, $actualversion, 'guestbook');              
    }

    return $result;
}